package com.demo.controller;

import com.demo.model.User;
import com.demo.service.T1Service;
import com.demo.service.T2Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.BoundSetOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;

/**
 * @Auther: luobiao
 * @Date: 2020/11/30 22:27
 * @Description:
 */
@RestController
public class IndexController {
    @Autowired
    T1Service t1Service;

    @Autowired
    T2Service t2Service;

    @Autowired



    @Resource
    RedisTemplate<String, String> redisTemplate;

    private static final String PROXY_HOSPITAL = "PROXY_HOSPITAL";
    private static final String HOSPITAL_USER = "HOSPITAL_USER";
    private static final String USER_INFO = "USER_INFO";
    private static final long EXPIRE_TIME_MINUTES = 40;



    @RequestMapping("redisAdd")
    public Object redis(){
        List<String> hospitalList = new ArrayList<>();
        List<String> userList = new ArrayList<>();
        IntStream.range(0, 50).forEach(p->{
            String proxyId = UUID.randomUUID().toString();
            //插入代理->医院缓存
            BoundHashOperations<String,String,List> proxyHospitalOps=redisTemplate.boundHashOps(PROXY_HOSPITAL);
            proxyHospitalOps.expire(EXPIRE_TIME_MINUTES, TimeUnit.MINUTES);
            List tempList = new ArrayList();
            IntStream.range(0, 20).forEach(r->{
                String hospitalId = UUID.randomUUID().toString();
                tempList.add(hospitalId);
                proxyHospitalOps.put(proxyId, tempList);
            });
            hospitalList.addAll(tempList);
        });

        //插入医院->用户缓存
        BoundHashOperations<String,String,List> hospitalUserOps=redisTemplate.boundHashOps(HOSPITAL_USER);
        hospitalUserOps.expire(EXPIRE_TIME_MINUTES, TimeUnit.MINUTES);
        hospitalList.stream().forEach(h->{
            List tempList = new ArrayList();
            IntStream.range(0,10).forEach(r->{
                String userId = UUID.randomUUID().toString();
                tempList.add(userId);
                hospitalUserOps.put(h,tempList);
            });
            userList.addAll(tempList);
        });

        //插入用户信息缓存
        BoundHashOperations<String,String,User> userInfoOps=redisTemplate.boundHashOps(USER_INFO);
        hospitalUserOps.expire(EXPIRE_TIME_MINUTES, TimeUnit.MINUTES);
        userList.stream().forEach(u->{
            userInfoOps.put(u, new User("_name_" + u, 0));
        });
        return null;
    }

    @RequestMapping("searchHospitalByProxyId")
    public Object redisSearch(String proxyId){
        long beginTime = System.currentTimeMillis();
        BoundHashOperations<String,String,List<String>> proxyHospitalOps=redisTemplate.boundHashOps(PROXY_HOSPITAL);
        List<String> hospitalIdList=proxyHospitalOps.get(proxyId);
        BoundHashOperations<String,String,List<String>> hospitalUserOps=redisTemplate.boundHashOps(HOSPITAL_USER);
        List<String> userIdList = new ArrayList<>();
        hospitalUserOps.multiGet(hospitalIdList).stream().forEach(h->{
            h.stream().forEach(u->{
                userIdList.add(u);
            });
        });
        BoundHashOperations<String,String,User> userInfoOps=redisTemplate.boundHashOps(USER_INFO);
        List<User> userList = userInfoOps.multiGet(userIdList);
        System.out.println(System.currentTimeMillis()-beginTime);
        return userList;
    }

    @RequestMapping("test")
    public Object test(){
        BoundListOperations listOperations=redisTemplate.boundListOps("key2");
        listOperations.expire(1, TimeUnit.MINUTES);
        String[] arr = new String[]{"one","two"};
        listOperations.rightPushAll(arr);
        Object ret1=listOperations.index(0);
        Object ret2=listOperations.index(1);
        Object ret3=listOperations.index(2);
        System.out.println(ret1);
        System.out.println(ret2);
        System.out.println(ret3);
        return ret2;
    }

    @RequestMapping("test2")
    public Object test2(){
        BoundSetOperations<String,String> listOperations=redisTemplate.boundSetOps("key2");;
        listOperations.expire(10, TimeUnit.MINUTES);
        return null;
    }

    @RequestMapping("index1")
    public Object index1(){
        return t1Service.list();
    }
    @RequestMapping("index2")
    public Object index2(){
        return t2Service.list();
    }
}
